import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;

@SuppressWarnings("serial")
class MainInterface extends javax.swing.JFrame {

        // Variables declaration for the GUI
        private javax.swing.JScrollPane jScrollPane;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanel2;
        private javax.swing.JButton buttonAddFiles;
        private javax.swing.JButton buttonCancel;
        private javax.swing.JButton buttonChoose1;
        private javax.swing.JButton buttonChoose2;
        private javax.swing.JButton buttonChoose3;
        private javax.swing.JButton buttonChoose4;
        private javax.swing.JButton buttonChoose5;
        private javax.swing.JButton buttonChoose6;
        private javax.swing.JButton buttonChoose7;
        private javax.swing.JButton buttonChoose8;
        private javax.swing.JButton buttonChoose9;
        private javax.swing.JButton buttonContinue;
        private javax.swing.JLabel jLabelFile1;
        private javax.swing.JLabel jLabelFile2;
        private javax.swing.JLabel jLabelFile3;
        private javax.swing.JLabel jLabelFile4;
        private javax.swing.JLabel jLabelFile5;
        private javax.swing.JLabel jLabelFile6;
        private javax.swing.JLabel jLabelFile7;
        private javax.swing.JLabel jLabelFile8;
        private javax.swing.JLabel jLabelFile9;
        private javax.swing.JLabel jLabelTitle;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField jTextField2;
        private javax.swing.JTextField jTextField3;
        private javax.swing.JTextField jTextField4;
        private javax.swing.JTextField jTextField5;
        private javax.swing.JTextField jTextField6;
        private javax.swing.JTextField jTextField7;
        private javax.swing.JTextField jTextField8;
        private javax.swing.JTextField jTextField9;

        // User Variables list
        LinkedList<java.io.File> listOfBam = new LinkedList<java.io.File>();
        LinkedList<java.io.File> listOfBai = new LinkedList<java.io.File>();
        int numberOfFiles;
        java.io.File pathGenBankFile;

        /**
         * Creates new form NewJFrame
         */
        public MainInterface() {
                initComponents();
        }

        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */

        private void initComponents() {

                this.numberOfFiles = 5;
                buttonCancel = new javax.swing.JButton();
                buttonContinue = new javax.swing.JButton();
                jLabelTitle = new javax.swing.JLabel();
                jPanel1 = new javax.swing.JPanel();
                jPanel2 = new javax.swing.JPanel();

                jLabelFile1 = new javax.swing.JLabel();
                jLabelFile2 = new javax.swing.JLabel();
                jLabelFile3 = new javax.swing.JLabel();
                jLabelFile4 = new javax.swing.JLabel();
                jLabelFile5 = new javax.swing.JLabel();
                jLabelFile6 = new javax.swing.JLabel();
                jLabelFile7 = new javax.swing.JLabel();
                jLabelFile8 = new javax.swing.JLabel();
                jLabelFile9 = new javax.swing.JLabel();

                jTextField1 = new javax.swing.JTextField();
                jTextField2 = new javax.swing.JTextField();
                jTextField3 = new javax.swing.JTextField();
                jTextField4 = new javax.swing.JTextField();
                jTextField5 = new javax.swing.JTextField();
                jTextField6 = new javax.swing.JTextField();
                jTextField7 = new javax.swing.JTextField();
                jTextField8 = new javax.swing.JTextField();
                jTextField9 = new javax.swing.JTextField();

                buttonChoose9 = new javax.swing.JButton();
                buttonChoose8 = new javax.swing.JButton();
                buttonChoose7 = new javax.swing.JButton();
                buttonChoose6 = new javax.swing.JButton();
                buttonChoose5 = new javax.swing.JButton();
                buttonChoose4 = new javax.swing.JButton();
                buttonChoose3 = new javax.swing.JButton();
                buttonChoose2 = new javax.swing.JButton();
                buttonChoose1 = new javax.swing.JButton();

                buttonAddFiles = new javax.swing.JButton();

                setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
                setTitle("TraVis - Transcriptome Visualization Software");
                setName("mainFrame");
                setMinimumSize(new java.awt.Dimension(550, 450));
                setResizable(false);
                this.setIconImage(java.awt.Toolkit.getDefaultToolkit().getImage(
                                new java.io.File("").getAbsolutePath() + "\\icons\\dna.png"));

                getContentPane().setLayout(null);

                buttonCancel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
                buttonCancel.setText("Cancel");
                getContentPane().add(buttonCancel);
                buttonCancel.setBounds(295, 366, 83, 33);
                buttonCancel.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                buttonCancelActionPerformed(evt);
                        }
                });

                buttonContinue.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
                buttonContinue.setText("Continue");
                getContentPane().add(buttonContinue);
                buttonContinue.setBounds(388, 366, 89, 33);
                buttonContinue.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                buttonContinueActionPerformed(evt);
                        }
                });

                jLabelTitle.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
                jLabelTitle.setText("Select the files and press \"Continue\" :");
                getContentPane().add(jLabelTitle);
                jLabelTitle.setBounds(10, 27, 335, 35);

                jPanel1.setBorder(javax.swing.BorderFactory
                                .createBevelBorder(javax.swing.border.BevelBorder.RAISED));
                jPanel1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
                jPanel1.setLayout(null);

                // jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
                jPanel2.setLayout(null);
                jPanel2.setPreferredSize(new Dimension(500, 200));

                buttonChoose1.setText("...");
                buttonChoose1.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                buttonChoose1ActionPerformed(evt);
                        }
                });
                jPanel2.add(buttonChoose1);
                buttonChoose1.setBounds(418, 12, 29, 23);

                buttonChoose3.setText("...");
                buttonChoose3.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                buttonChoose3ActionPerformed(evt);
                        }
                });
                jPanel2.add(buttonChoose3);
                buttonChoose3.setBounds(418, 82, 29, 23);

                buttonChoose2.setText("...");
                buttonChoose2.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                buttonChoose2ActionPerformed(evt);
                        }
                });
                jPanel2.add(buttonChoose2);
                buttonChoose2.setBounds(418, 47, 29, 23);

                buttonChoose5.setText("...");
                jPanel2.add(buttonChoose5);
                buttonChoose5.setBounds(418, 152, 29, 23);
                buttonChoose5.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                buttonChoose5ActionPerformed(evt);
                        }
                });

                buttonChoose4.setText("...");
                buttonChoose4.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                buttonChoose4ActionPerformed(evt);
                        }
                });
                jPanel2.add(buttonChoose4);
                buttonChoose4.setBounds(418, 117, 29, 23);

                jTextField2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
                jPanel2.add(jTextField2);
                jTextField2.setBounds(111, 46, 275, 23);

                jTextField3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

                jPanel2.add(jTextField3);
                jTextField3.setBounds(111, 81, 275, 23);

                jTextField5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

                jPanel2.add(jTextField5);
                jTextField5.setBounds(111, 151, 275, 23);

                jTextField1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
                jPanel2.add(jTextField1);
                jTextField1.setBounds(111, 11, 275, 23);
                jTextField1.setEditable(false);
                jTextField2.setEditable(false);
                jTextField3.setEditable(false);
                jTextField4.setEditable(false);
                jTextField5.setEditable(false);
                jTextField6.setEditable(false);
                jTextField7.setEditable(false);
                jTextField8.setEditable(false);
                jTextField9.setEditable(false);

                jTextField4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
                jPanel2.add(jTextField4);
                jTextField4.setBounds(111, 116, 275, 23);

                jLabelFile4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
                jLabelFile4.setText("Bam File:");
                jPanel2.add(jLabelFile4);
                jLabelFile4.setBounds(10, 120, 48, 15);

                jLabelFile5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
                jLabelFile5.setText("Bam Index File:");
                jPanel2.add(jLabelFile5);
                jLabelFile5.setBounds(10, 151, 83, 23);

                jLabelFile2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
                jLabelFile2.setText("Bam File:");
                jPanel2.add(jLabelFile2);
                jLabelFile2.setBounds(10, 50, 48, 15);

                jLabelFile3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
                jLabelFile3.setText("Bam Index File:");
                jPanel2.add(jLabelFile3);
                jLabelFile3.setBounds(10, 81, 83, 23);

                jLabelFile1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
                jLabelFile1.setText("GenBank File :");
                jPanel2.add(jLabelFile1);
                jLabelFile1.setBounds(10, 15, 77, 15);

                // jScrollPane.setLayout(null);

                // jScrollPane.add(jPanel2);
                jScrollPane = new javax.swing.JScrollPane(jPanel2);
                jScrollPane.setBounds(12, 13, 500, 200);
                jScrollPane.setViewportView(jPanel2);

                jPanel1.add(jScrollPane);

                buttonAddFiles.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
                buttonAddFiles.setText("Add files...");
                buttonAddFiles.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                buttonAddFilesActionPerformed(evt);
                        }
                });
                jPanel1.add(buttonAddFiles);
                buttonAddFiles.setBounds(285, 222, 174, 33);

                getContentPane().add(jPanel1);
                jPanel1.setBounds(10, 80, 524, 272);

                pack();
        }

        private void buttonChoose1ActionPerformed(java.awt.event.ActionEvent evt) {
                this.pathGenBankFile = getFile("gb");
                try {
                        if (this.pathGenBankFile != null) {

                                this.jTextField1.setText(pathGenBankFile.getCanonicalPath());
                        } else
                                return;
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }

        private void buttonChoose2ActionPerformed(ActionEvent evt) {
                this.listOfBam.add(getFile("bam"));

                try {
                        if (this.listOfBam.get(this.listOfBam.size() - 1) != null) {

                                this.jTextField2.setText(this.listOfBam.get(
                                                this.listOfBam.size() - 1).getCanonicalPath());
                        } else {
                                this.listOfBam.remove(this.listOfBam.size() - 1);
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                }

        }

        private void buttonChoose3ActionPerformed(ActionEvent evt) {
                this.listOfBai.add(getFile("bai"));

                try {
                        if (this.listOfBai.get(this.listOfBai.size() - 1) != null) {

                                this.jTextField3.setText(this.listOfBai.get(
                                                this.listOfBai.size() - 1).getCanonicalPath());
                        } else {
                                this.listOfBai.remove(this.listOfBai.size() - 1);
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                }

        }

        private void buttonChoose4ActionPerformed(ActionEvent evt) {
                this.listOfBam.add(getFile("bam"));

                try {
                        if (this.listOfBam.get(this.listOfBam.size() - 1) != null) {

                                this.jTextField4.setText(this.listOfBam.get(
                                                this.listOfBam.size() - 1).getCanonicalPath());
                        } else {
                                this.listOfBam.remove(this.listOfBam.size() - 1);
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                }

        }

        private void buttonChoose5ActionPerformed(ActionEvent evt) {
                this.listOfBai.add(getFile("bai"));

                try {
                        if (this.listOfBai.get(this.listOfBai.size() - 1) != null) {

                                this.jTextField5.setText(this.listOfBai.get(
                                                this.listOfBai.size() - 1).getCanonicalPath());
                        } else {
                                this.listOfBai.remove(this.listOfBai.size() - 1);
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                }

        }

        private void buttonChoose6ActionPerformed(ActionEvent evt) {
                this.listOfBam.add(getFile("bam"));

                try {
                        if (this.listOfBam.get(this.listOfBam.size() - 1) != null) {

                                this.jTextField6.setText(this.listOfBam.get(
                                                this.listOfBam.size() - 1).getCanonicalPath());
                        } else {
                                this.listOfBam.remove(this.listOfBam.size() - 1);
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                }

        }

        private void buttonChoose7ActionPerformed(ActionEvent evt) {
                this.listOfBai.add(getFile("bai"));

                try {
                        if (this.listOfBai.get(this.listOfBai.size() - 1) != null) {

                                this.jTextField7.setText(this.listOfBai.get(
                                                this.listOfBai.size() - 1).getCanonicalPath());
                        } else {
                                this.listOfBai.remove(this.listOfBai.size() - 1);
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                }

        }

        private void buttonChoose8ActionPerformed(ActionEvent evt) {
                this.listOfBam.add(getFile("bam"));

                try {
                        if (this.listOfBam.get(this.listOfBam.size() - 1) != null) {

                                this.jTextField8.setText(this.listOfBam.get(
                                                this.listOfBam.size() - 1).getCanonicalPath());
                        } else {
                                this.listOfBam.remove(this.listOfBam.size() - 1);
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }

        private void buttonChoose9ActionPerformed(ActionEvent evt) {
                this.listOfBai.add(getFile("bai"));

                try {
                        if (this.listOfBai.get(this.listOfBai.size() - 1) != null) {

                                this.jTextField9.setText(this.listOfBai.get(
                                                this.listOfBai.size() - 1).getCanonicalPath());
                        } else {
                                this.listOfBai.remove(this.listOfBai.size() - 1);
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                }

        }

        private void buttonAddFilesActionPerformed(ActionEvent evt) {

                if (numberOfFiles == 5) {

                        this.jTextField6.setSize(this.jTextField4.getSize());
                        this.jTextField6.setLocation(jTextField5.getLocation().x,
                                        jTextField5.getLocation().y + 38);
                        this.jPanel2.add(this.jTextField6);

                        this.jLabelFile6.setText(jLabelFile4.getText());
                        this.jLabelFile6.setBounds(jLabelFile5.getLocation().x,
                                        jLabelFile5.getLocation().y + 38,
                                        jLabelFile4.getWidth() + 10, jLabelFile4.getHeight());

                        this.jPanel2.add(this.jLabelFile6);

                        this.buttonChoose6.setSize(this.buttonChoose4.getSize());
                        this.buttonChoose6.setText(buttonChoose4.getText());
                        this.buttonChoose6.setLocation(buttonChoose5.getLocation().x,
                                        buttonChoose5.getLocation().y + 38);
                        buttonChoose6
                                        .addActionListener(new java.awt.event.ActionListener() {
                                                public void actionPerformed(
                                                                java.awt.event.ActionEvent evt) {
                                                        buttonChoose6ActionPerformed(evt);
                                                }
                                        });
                        this.jPanel2.add(this.buttonChoose6);
                        // part one

                        this.jTextField7.setSize(this.jTextField5.getSize());
                        this.jTextField7.setLocation(jTextField6.getLocation().x,
                                        jTextField6.getLocation().y + 38);
                        this.jPanel2.add(this.jTextField7);

                        this.jLabelFile7.setSize(this.jLabelFile5.getSize());
                        this.jLabelFile7.setBounds(jLabelFile6.getLocation().x,
                                        jLabelFile6.getLocation().y + 38,
                                        jLabelFile5.getWidth() + 10, jLabelFile5.getHeight());
                        this.jLabelFile7.setText(jLabelFile5.getText());
                        this.jPanel2.add(this.jLabelFile7);

                        this.buttonChoose7.setSize(this.buttonChoose5.getSize());
                        this.buttonChoose7.setText(buttonChoose5.getText());
                        this.buttonChoose7.setLocation(buttonChoose6.getLocation().x,
                                        buttonChoose6.getLocation().y + 38);
                        buttonChoose7
                                        .addActionListener(new java.awt.event.ActionListener() {
                                                public void actionPerformed(
                                                                java.awt.event.ActionEvent evt) {
                                                        buttonChoose7ActionPerformed(evt);
                                                }
                                        });
                        this.jPanel2.add(this.buttonChoose7);
                        jPanel2.setPreferredSize(new Dimension(500, 300));
                        numberOfFiles += 2;
                } else if (numberOfFiles == 7) {
                        this.jTextField8.setSize(this.jTextField6.getSize());
                        this.jTextField8.setLocation(jTextField7.getLocation().x,
                                        jTextField7.getLocation().y + 38);
                        this.jPanel2.add(this.jTextField8);

                        this.jLabelFile8.setText(jLabelFile6.getText());
                        this.jLabelFile8.setBounds(jLabelFile7.getLocation().x,
                                        jLabelFile7.getLocation().y + 38,
                                        jLabelFile6.getWidth() + 10, jLabelFile6.getHeight());

                        this.jPanel2.add(this.jLabelFile8);

                        this.buttonChoose8.setSize(this.buttonChoose6.getSize());
                        this.buttonChoose8.setText(buttonChoose6.getText());
                        this.buttonChoose8.setLocation(buttonChoose7.getLocation().x,
                                        buttonChoose7.getLocation().y + 38);
                        buttonChoose8
                                        .addActionListener(new java.awt.event.ActionListener() {
                                                public void actionPerformed(
                                                                java.awt.event.ActionEvent evt) {
                                                        buttonChoose8ActionPerformed(evt);
                                                }
                                        });
                        this.jPanel2.add(this.buttonChoose8);
                        // part one

                        this.jTextField9.setSize(this.jTextField7.getSize());
                        this.jTextField9.setLocation(jTextField8.getLocation().x,
                                        jTextField8.getLocation().y + 38);
                        this.jPanel2.add(this.jTextField9);

                        this.jLabelFile9.setSize(this.jLabelFile7.getSize());
                        this.jLabelFile9.setBounds(jLabelFile8.getLocation().x,
                                        jLabelFile8.getLocation().y + 38,
                                        jLabelFile7.getWidth() + 10, jLabelFile7.getHeight());
                        this.jLabelFile9.setText(jLabelFile7.getText());
                        this.jPanel2.add(this.jLabelFile9);

                        this.buttonChoose9.setSize(this.buttonChoose7.getSize());
                        this.buttonChoose9.setText(buttonChoose7.getText());
                        this.buttonChoose9.setLocation(buttonChoose8.getLocation().x,
                                        buttonChoose8.getLocation().y + 38);
                        buttonChoose9
                                        .addActionListener(new java.awt.event.ActionListener() {
                                                public void actionPerformed(
                                                                java.awt.event.ActionEvent evt) {
                                                        buttonChoose9ActionPerformed(evt);
                                                }
                                        });
                        this.jPanel2.add(this.buttonChoose9);
                        jPanel2.setPreferredSize(new Dimension(500, 350));
                        numberOfFiles += 2;

                } else if (numberOfFiles >= 9) {
                        mostrarAviso("Error", "You already load the number max. of files.");
                }

                this.repaint();
                this.validate();

        }

        public boolean verifyFile() {
                if (this.listOfBai.size() != this.listOfBam.size()) {
                        System.out.println(this.listOfBam.size());
                        System.out.println(this.listOfBai.size());
                        mostrarAviso("Error",
                                        "The number of .bam Files and .bai files must be equal!");
                        return false;
                }

                if (pathGenBankFile == null) {
                        mostrarAviso("Error", "You should load a GenBank File.");
                        return false;
                }
                 if (this.listOfBam.size() < 2) {
                 mostrarAviso("Error", "You should upload at list two BAM Files.");
                 return false;
                 }
                return true;
        }

        private void buttonCancelActionPerformed(ActionEvent evt) {
                System.exit(0);

        }

        private void buttonContinueActionPerformed(ActionEvent evt) {
                if (!verifyFile())
                        return;
                ArrayList<Species> species = new ArrayList<Species>();
                for (int i = 0; i < ((numberOfFiles-1)/2); i++) {
                        Species specie1 = null;
                                 specie1 = new Species(this.listOfBam.remove(),
                                                this.listOfBai.remove(), new GenBankParser(pathGenBankFile));

                        species.add(specie1);
                }
               

                new Graphics(species.size(), "TraVis - Transcriptome Visualization Software", species);
        }

        private java.io.File getFile(String ext1) // abre janela para seleo de
                                                                                                // arquivo
        {
                javax.swing.JFileChooser path = new javax.swing.JFileChooser(".");
                path.setMultiSelectionEnabled(false);
                path.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter(
                                ext1, ext1));
                if (path.showOpenDialog(null) != javax.swing.JFileChooser.APPROVE_OPTION)
                        return (null);
                return (path.getSelectedFile());
        }

        public static void mostrarAviso(String title, String text) // gera janela de
                                                                                                                                // aviso
        {
                javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(),
                                text, title, javax.swing.JOptionPane.INFORMATION_MESSAGE);
        }

        public static void main(String args[]) {
                try {
                        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
                                        .getInstalledLookAndFeels()) {
                                if ("Nimbus".equals(info.getName())) {
                                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                                        break;
                                }
                        }
                } catch (ClassNotFoundException ex) {
                        java.util.logging.Logger.getLogger(MainInterface.class.getName())
                                        .log(java.util.logging.Level.SEVERE, null, ex);
                } catch (InstantiationException ex) {
                        java.util.logging.Logger.getLogger(MainInterface.class.getName())
                                        .log(java.util.logging.Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                        java.util.logging.Logger.getLogger(MainInterface.class.getName())
                                        .log(java.util.logging.Level.SEVERE, null, ex);
                } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                        java.util.logging.Logger.getLogger(MainInterface.class.getName())
                                        .log(java.util.logging.Level.SEVERE, null, ex);
                }
                /* paass! */

                /* Create and display the form */
                java.awt.EventQueue.invokeLater(new Runnable() {
                        public void run() {
                                new MainInterface().setVisible(true);
                        }
                });
        }

}

